﻿# encoding=utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException
import time, unittest, re
# 登录页面
driver = webdriver.Chrome()
driver.get("https://login.taobao.com")
# 进入账户密码模式
driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div/div/div[2]/div[4]/div/div[5]/a').click()
driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div/div/div[2]/div[3]/form/div[2]/span').click()
driver.find_element_by_id('TPL_username_1').send_keys('toahua')
# 账户名输入用的xpath，密码输入用By，皮一下
driver.find_element_by_id("TPL_password_1").click()
driver.find_element_by_id("TPL_password_1").send_keys('Cbf123456')
driver.find_element_by_id("J_SubmitStatic").click()  # 登录摁扭
#至此按理说如果是用手工的话就已经可以登录了 ，然而在跑自动化时发现出现了滑块
#所以需要再次输入密码并且滑动滑块
# 需要滑块，再次登录，先输入密码，再滑动滑块
driver.find_element_by_id("TPL_password_1").click()
driver.find_element_by_id("TPL_password_1").send_keys('Cbf123456')
time.sleep(1)
dragger = driver.find_element_by_id('nc_1_n1z')  # .滑块定位
action = ActionChains(driver)
for index in range(500):
    try:
        action.drag_and_drop_by_offset(dragger, 500, 0).perform()  # 平行移动鼠标，此处直接设一个超出范围的值，这样拉到头后会报错从而结束这个动作
    except UnexpectedAlertPresentException:
        break
    time.sleep(11)  # 等待停顿时间
driver.find_element_by_id('J_SubmitStatic').click()  # 重新摁登录摁扭
print("finish")